//
//  statusCell.swift
//  MicroBlog
//
//  Created by QT on 15/8/2.
//  Copyright © 2015年 綦敦涛. All rights reserved.
//

import UIKit

enum statusCellIdentifier: String{
    case NormalCell = "NormalCell"
    case ForwardCell = "ForwardCell"
    static func cellID(Status: status) -> String{
        return Status.retweeted_status == nil ? statusCellIdentifier.NormalCell.rawValue : statusCellIdentifier.ForwardCell.rawValue
    }
    
}

class statusCell: UITableViewCell {
    
    var Status:status?{
        didSet{
            topView.Tstatus = Status
            content.text = Status?.text
            pictureView.PictureStatus = Status
            
            
            widthConstraint?.constant = pictureView.bounds.width
            heightConstraint?.constant = pictureView.bounds.height
            

        }
    }
    
    var widthConstraint: NSLayoutConstraint?
    var heightConstraint: NSLayoutConstraint?
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        // 添加自控件
        prepareUI()
        // 布局子控件
        layoutUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 获得cell高度
    func getRowHeight(Status: status) -> CGFloat{
        self.Status = Status
        layoutIfNeeded()
        let hi = CGRectGetMaxY(bottomView.frame)
        
        return hi
    }
    
    // 添加子控件
    func prepareUI(){
        contentView.addSubview(topView)
        contentView.addSubview(content)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
    }
    // 布局子控件
    private func layoutUI(){
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 67))
        
        
        content.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: 8, y: 8))
        contentView.addConstraint(NSLayoutConstraint(item: content, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: -16))
        
//        let cons = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: content, size: CGSize(width: 290, height: 290), offset: CGPoint(x: 0, y: 8))
//        
//        widthConstraint = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
//        heightConstraint = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
        
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -8, y: 8))
//    
//        // 底部约束
//        contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant:0))
    }
    
    // MARK: - 懒加载
    
    private lazy var topView: statusCellTop = statusCellTop()
    // 懒加载文本框
    lazy var content: UILabel = {
        let lab = UILabel(color: UIColor.darkGrayColor(), fontSize: 15)
        lab.numberOfLines = 0
        lab.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 16
        return lab
    }()
    
    lazy var bottomView: statusCellBottom = statusCellBottom()

    lazy var pictureView: statusPictureView = statusPictureView()
    
    
}
